﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Messenger;

namespace MSN {
    public class Bluetooth : IMessengerAddIn {
        #region IMessengerAddIn Members

        public Bluetooth() {
            
        }
        MessengerClient messenger = null;
        void IMessengerAddIn.Initialize(MessengerClient messenger) {
            this.messenger = messenger;
            messenger.AddInProperties.FriendlyName = "MSN Bluetooth Abilities 1";
            messenger.AddInProperties.Creator = "Rob CHartier";
            messenger.AddInProperties.Description = "Allow MSN to interact with Bluetooh devices.";
            //bug:messenger.AddInProperties.Url = new Uri("http://weblogs.asp.net/rchartier");
            this.messenger.ShowOptionsDialog += new EventHandler(messenger_ShowOptionsDialog);
            this.messenger.Shutdown += new EventHandler(messenger_Shutdown);
            this.messenger.AddInProperties.PersonalStatusMessage = string.Format("{0} (BT)", this.messenger.AddInProperties.PersonalStatusMessage);
            BluetoothListener.OnDeviceChanged += new BluetoothListener.DeviceChanged(BluetoothListener_OnDeviceChanged);
        }

        void BluetoothListener_OnDeviceChanged(InTheHand.Net.Sockets.BluetoothDeviceInfo Device, bool Connected) {
            if(Connected) {
                this.messenger.AddInProperties.Status = UserStatus.Online;
            } else {
                this.messenger.AddInProperties.Status = UserStatus.Away;
            }
        }

        void messenger_Shutdown(object sender, EventArgs e) {
            BluetoothListener.Disconnect();
        }

        void messenger_ShowOptionsDialog(object sender, EventArgs e) {
            try {
                BluetoothListener.ChooseDevice();
            } catch(Exception exc) {
                System.Windows.Forms.MessageBox.Show(exc.ToString());
            }
        }


        #endregion
    }
}
